package com.spic.system.controller;

import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.domain.HrUser;
import com.spic.system.service.IHrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author 290512
 * @create 2022-06-09 14:45
 */
@Slf4j
@RestController
@RequestMapping("/hr")
public class HrController extends BaseController
{
    @Autowired
    private IHrService hrUserService;

    /**
     * 查询hr_user列表
     */
    @PreAuthorize(hasPermi = "business:hr:list")
    @GetMapping("/list")
    public TableDataInfo list(HrUser hrUser)
    {
        startPage();
        List<HrUser> list = hrUserService.selectHrUserList(hrUser);
        return getDataTable(list);
    }

    /**
     * 导出hr_user列表
     */
    @PreAuthorize(hasPermi = "business:hr:export")
    @Log(title = "hr_user", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HrUser hrUser) throws IOException
    {
        List<HrUser> list = hrUserService.selectHrUserList(hrUser);
        ExcelUtil<HrUser> util = new ExcelUtil<HrUser>(HrUser.class);
        util.exportExcel(response, list, "hr_user数据");
    }

    /**
     * 获取hr_user详细信息
     */
    @PreAuthorize(hasPermi = "business:hr:query")
    @GetMapping(value = "/{personnelCode}")
    public AjaxResult getInfo(@PathVariable("personnelCode") String personnelCode)
    {
        return AjaxResult.success(hrUserService.selectHrUserByPersonnelCode(personnelCode));
    }

    /**
     * 新增hr_user
     */
    @PreAuthorize(hasPermi = "business:hr:add")
    @Log(title = "hr_user", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HrUser hrUser)
    {
        return toAjax(hrUserService.insertHrUser(hrUser));
    }

    /**
     * 修改hr_user
     */
    @PreAuthorize(hasPermi = "business:hr:edit")
    @Log(title = "hr_user", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HrUser hrUser)
    {
        return toAjax(hrUserService.updateHrUser(hrUser));
    }

    /**
     * 删除hr_user
     */
    @PreAuthorize(hasPermi = "business:hr:remove")
    @Log(title = "hr_user", businessType = BusinessType.DELETE)
    @DeleteMapping("/{personnelCodes}")
    public AjaxResult remove(@PathVariable String[] personnelCodes)
    {
        return toAjax(hrUserService.deleteHrUserByPersonnelCodes(personnelCodes));
    }

    @PostMapping(value = "/importData")
    public AjaxResult importData(@RequestPart(value = "file", required = true) MultipartFile file) throws Exception
    {
        ExcelUtil<HrUser> util = new ExcelUtil<HrUser>(HrUser.class);
        List<HrUser> hrUserList = util.importExcel(file.getInputStream());
        String message = hrUserService.importData(hrUserList);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        try(ServletOutputStream servletOutputStream = response.getOutputStream()){
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("HR导入模板.xls","utf8"));
            ClassPathResource classPathResource = new ClassPathResource("Excel_model/HR导入模板.xls");
            InputStream inputStream = classPathResource.getInputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        }catch (IOException e){
            log.info(e.getMessage());

        }catch (RuntimeException e) {
            log.info(e.getMessage());
        }

    }

    @PostMapping(value = "/synchronizationHr")
    public AjaxResult synchronizationHr(){
        hrUserService.synchronizationHr();
        return AjaxResult.success();
    }
}
